﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_module_list_usergroup : System.Web.UI.UserControl
{
    user_groupBus usergroupbus = new user_groupBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getUserGroup();
        }
    }

    public void getUserGroup()
    {
        DataTable dt = usergroupbus.getAllUserGroup();
        DataTable list_usergroup = new DataTable();
        list_usergroup.Columns.Add("id");
        list_usergroup.Columns.Add("name");
        list_usergroup.Columns.Add("href");
        DataRow dr;
        foreach (DataRow r in dt.Rows)
        {
            dr = list_usergroup.NewRow();
            dr["id"] = r["user_group_id"];
            dr["name"] = r["name"];
            dr["href"] = "~//admin//default.aspx?action=usergroup&id=" + Convert.ToInt32(r["user_group_id"]);
            list_usergroup.Rows.Add(dr);
        }

        listusergroup.DataSource = list_usergroup;
        listusergroup.DataBind();
        if (list_usergroup.Rows.Count == 0)
        {
            text_empty.Visible = true;
        }
    }

    protected void list_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(listusergroup.DataKeys[e.Item.ItemIndex]);
        if (!usergroupbus.DeleteUserGroup(id))
        {
            txt_error.Text = "Nhóm người dùng không thể xóa do đang sử dụng";
        }
        getUserGroup();

    }
}